import objc
from Foundation import *
from AppKit import *
from PyObjCTools import NibClassBuilder

# class defined in MainMenu.nib
class DBInklingDrawerWindow(NibClassBuilder.AutoBaseClass):
    # the actual base class is NSWindow
    # The following outlets are added to the class:
    # bottomDrawer
    # leftDrawer
    # rightDrawer
    # topDrawer
    
    def awakeFromNib(self):
        if self.leftDrawer:
            self.leftDrawer.position_inParent_('left', self)
        if self.rightDrawer:
            self.rightDrawer.position_inParent_('right', self)
        if self.topDrawer:
            self.topDrawer.position_inParent_('top', self)
        if self.bottomDrawer:
            self.bottomDrawer.position_inParent_('bottom', self)
        

# class defined in MainMenu.nib
class DBInklingDrawer(NibClassBuilder.AutoBaseClass):
    
    animation = None
    animationDuration = 0.125
    
    def position_inParent_(self, pos, parent):
        self.position = pos
        self.isOpen = False
        parent.contentView().addSubview_(self)
        (ox,oy),(parent_width, parent_height) = parent.contentView().frame()
        (x,y),(width,height) = self.frame()
        centered_x = (parent_width - width) / 2
        centered_y = (parent_height - height) / 2
        closed_x = {'left': 10 - width, 'right': parent_width - 10, 'top': centered_x, 'bottom': centered_x}[pos]
        open_x = {'left': -10, 'right': parent_width - width + 10, 'top': centered_x, 'bottom': centered_x}[pos]
        closed_y = {'left': centered_y, 'right': centered_y, 'top': parent_height - 10, 'bottom': 10 - height}[pos]
        open_y = {'left': centered_y, 'right': centered_y, 'top': parent_height - height + 10, 'bottom': -10}[pos]
        self.closedPosition = NSValue.valueWithRect_(NSMakeRect(closed_x, closed_y, width, height))
        self.openPosition = NSValue.valueWithRect_(NSMakeRect(open_x, open_y, width, height))
        self.setFrame_(((closed_x, closed_y),(width,height)))
        self.trackingTag = self.addTrackingRect_owner_userData_assumeInside_(self.visibleRect(), self, 0, False)
                
    def mouseEntered_(self, theEvent):
        if not self.isOpen:
            self.removeTrackingRect_(self.trackingTag)
            self.slideOpen()
        
    def mouseExited_(self, theEvent):
        if self.isOpen:
            self.removeTrackingRect_(self.trackingTag)
            self.slideClosed()
    
    def animationDidEnd_(self, animation):
        self.animation = None
        self.isOpen = animation.isOpening()    
        self.trackingTag = self.addTrackingRect_owner_userData_assumeInside_(self.visibleRect(), self, 0, self.isOpen)
        
    def slideOpen(self):
        self.animation = DBInklingAnimation.alloc().initWithViewAnimations_([{NSViewAnimationTargetKey: self,
            NSViewAnimationStartFrameKey: self.closedPosition,
            NSViewAnimationEndFrameKey: self.openPosition}])
        self.animation.setIsOpening_(True)
        self.animation.setDelegate_(self)
        self.animation.setDuration_(self.animationDuration)
        self.animation.startAnimation()
            
    def slideClosed(self):
        self.animation = DBInklingAnimation.alloc().initWithViewAnimations_([{NSViewAnimationTargetKey: self,
            NSViewAnimationStartFrameKey: self.openPosition,
            NSViewAnimationEndFrameKey: self.closedPosition}])
        self.animation.setIsOpening_(False)
        self.animation.setDelegate_(self)
        self.animation.setDuration_(self.animationDuration)
        self.animation.startAnimation()
    
    def copyWithZone_(self, zone):
        return self
    copyWithZone_ = objc.selector(copyWithZone_, signature=NSObject.copyWithZone_.signature)


class DBInklingAnimation(NSViewAnimation):
    _isOpening = True
    def setIsOpening_(self, flag):
        self._isOpening = flag
        
    def isOpening(self):
        return self._isOpening

class DBBackgroundView(NibClassBuilder.AutoBaseClass):
    
    def drawRect_(self, rect):
        NSColor.blackColor().set()
        NSRectFill(rect)